//
//  KZVocabularyImproveTestResultView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/8/22.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZVocabularyImproveTestResultView.h"

#import "KZShadowButton.h"

/**
 记录做题结果的视图
 1.全部合格，底部对应按钮：“关闭窗口”，“下一个词汇包”
 2.未能全部合格，底部对应按钮：“查看不合格词汇”，“下一个词汇包”，并在训练时长下面增加去个人错题本的超链接按钮
 */
@implementation KZVocabularyImproveTestResultView
{
    
    // 所有状态均存在
    UIImageView             *_statusView;               // 状态gif
    UIButton                *_successCountView;         // 合格数量
    UIButton                *_failureCountView;         // 不合格数量
//    LSTYAttributedLabel     *_failureTestArchivedLabel; // 错题归档
    
    KZShadowButton          *_leftButton;
    KZShadowButton          *_rightButton;
}

- (instancetype)initWithReviewType:(int)reviewType {
    
    if (self = [super init]) {
        
        _reviewType = reviewType;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    _statusView = [UIImageView new];
    _statusView.userInteractionEnabled = true;
    _statusView.image = [UIImage imageNamed:@"kz-训练未能通过鸽子背景"];
    [self addSubview:_statusView];
    [_statusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@50);
        make.centerX.equalTo(@0);
        make.width.equalTo(self).multipliedBy(0.7);
        make.height.equalTo(_statusView.mas_width).multipliedBy(285.0/512);
    }];
    
    _successCountView = [UIButton buttonWithType:UIButtonTypeCustom];
    [_successCountView setBackgroundImage:[UIImage imageNamed:@"kz-合格数量背景"] forState:UIControlStateNormal];
    [self addSubview:_successCountView];
    [_successCountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_statusView.mas_bottom).offset(10);
        make.right.equalTo(self.mas_centerX).offset(-30);
    }];
    
    
    
    
    UILabel *successNoteLabel = [[UILabel alloc] init];
    successNoteLabel.text = @"合格数量";
    successNoteLabel.font = FONT(13);
    successNoteLabel.textAlignment = NSTextAlignmentCenter;
    successNoteLabel.textColor = [UIColor grayColor];
    [self addSubview:successNoteLabel];
    [successNoteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_successCountView);
        make.top.equalTo(_successCountView.mas_bottom).offset(10);
    }];
    
    _failureCountView = [UIButton buttonWithType:UIButtonTypeCustom];
    [_failureCountView setBackgroundImage:[UIImage imageNamed:@"kz-不合格数量背景"] forState:UIControlStateNormal];
    [self addSubview:_failureCountView];
    [_failureCountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_successCountView);
        make.left.equalTo(self.mas_centerX).offset(30);
    }];
    
    UILabel *failureNoteLabel = [[UILabel alloc] init];
    failureNoteLabel.text = @"不合格数量";
    failureNoteLabel.font = FONT(13);
    failureNoteLabel.textAlignment = NSTextAlignmentCenter;
    failureNoteLabel.textColor = [UIColor grayColor];
    [self addSubview:failureNoteLabel];
    [failureNoteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_failureCountView);
        make.top.equalTo(_failureCountView.mas_bottom).offset(10);
    }];
    
//    _failureTestArchivedLabel = [[LSTYAttributedLabel alloc] init];
//    _failureTestArchivedLabel.delegate = self;
//    [self addSubview:_failureTestArchivedLabel];
//    _failureTestArchivedLabel.textColor = UIColor.grayColor;
//    _failureTestArchivedLabel.font = [UIFont systemFontOfSize:13];
//    _failureTestArchivedLabel.text = [NSString stringWithFormat:@"答错的试题已经自动归入你的%@!", _reviewType == 0 ? @"个人生词本" : @"个人错题本"];
//    _failureTestArchivedLabel.textAlignment = 2;
//    [_failureTestArchivedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(@0);
//        make.top.equalTo(successNoteLabel.mas_bottom).offset(10);
//        make.height.equalTo(@30);
//    }];
//    LSTYLinkTextStorage *linkTextStorage = [[LSTYLinkTextStorage alloc] init];
//    linkTextStorage.underLineStyle = kCTUnderlineStyleSingle;
//    linkTextStorage.range = NSMakeRange(13, 5);
//    linkTextStorage.font = [UIFont systemFontOfSize:16];
//    linkTextStorage.textColor = KZThemeColor;
//    linkTextStorage.linkData = @"我被点中了哦O(∩_∩)O~";
//    [_failureTestArchivedLabel addTextStorage:linkTextStorage];
    
    
   
    _leftButton = [[KZShadowButton alloc] init:^(UIButton *button) {
        button.backgroundColor = KZThemeColor2;
    }];
    [_leftButton addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_leftButton];
    [_leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_centerX).offset(-5);
        make.bottom.mas_equalTo(H(-20));
        make.left.mas_equalTo(W(20));
        make.height.mas_equalTo(H(40));
    }];
    
    _rightButton = [[KZShadowButton alloc] init:^(UIButton *button) {
        button.backgroundColor = KZThemeColor;
        [button setTitle:@"下一个词汇包" forState:UIControlStateNormal];
    }];
    [_rightButton addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_rightButton];
    [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_centerX).offset(5);
        make.bottom.width.height.equalTo(_leftButton);

    }];

}


- (void)shadow:(UIView *)view color:(UIColor *)color {
    
    view.layer.shadowColor = color.CGColor;
    view.layer.shadowRadius = 6;
    view.layer.shadowOpacity = 0.6;
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, view.height - 10, view.width, 20)];
    view.layer.shadowPath = path.CGPath;
}
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    NSInteger successCount = 0;
    NSInteger failureCount = 0;
    for (KZVocabularyCurrentPackageDetailModel *model in _packageModel.currentPackage.Detail) {
        if (model.IsPassed.boolValue == false) {
            failureCount ++;
        } else {
            successCount ++;
        }
    }
    
    if (failureCount > 0) {
        [_statusView gif:@"2-8_Failed to pass-Loop"];
        [_leftButton.button setTitle:@"查看不合格词汇" forState:UIControlStateNormal];
    } else {
        [_statusView gif:@"2-8_Passed-Loop"];
        [_leftButton.button setTitle:@"关闭窗口" forState:UIControlStateNormal];
    }
    [_successCountView setTitle:[NSString stringWithFormat:@"%li", successCount] forState:UIControlStateNormal];
    [_failureCountView setTitle:[NSString stringWithFormat:@"%li", failureCount] forState:UIControlStateNormal];
    
    
    [self shadow:_successCountView color:[UIColor colorWithR:90 g:191 b:73 a:1]];
    [self shadow:_failureCountView color:[UIColor colorWithR:220 g:100 b:43 a:1]];
}

//- (void)attributedLabel:(LSTYAttributedLabel *)attributedLabel textStorageClicked:(id<LSTYTextStorageProtocol>)textStorage atPoint:(CGPoint)point atRect:(CGRect)rect {
//    if (self.clickBlock) {
//        self.clickBlock(3);
//    }
//}

#pragma mark - 事件
- (void)leftAction {
    // 未能全部合格
    if ([_leftButton.button.titleLabel.text isEqualToString:@"查看不合格词汇"]) {
        if (self.clickBlock) {
            self.clickBlock(1);
        }
    }
    // 全部合格，关闭窗口
    else {
        if (self.clickBlock) {
            self.clickBlock(2);
        }
    }
}
// 下一个词汇包
- (void)rightAction {
    if (self.clickBlock) {
        self.clickBlock(0);
    }
}
@end
